/**
 * Copyright 2018-2019 jianggujin (www.jianggujin.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jianggujin.modulelink.action.http;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * HTTP请求处理
 * 
 * @author jianggujin
 *
 */
public abstract class JHttpAction extends JGenericHttpAction {
    private static final String METHOD_DELETE = "DELETE";
    private static final String METHOD_HEAD = "HEAD";
    private static final String METHOD_GET = "GET";
    private static final String METHOD_OPTIONS = "OPTIONS";
    private static final String METHOD_POST = "POST";
    private static final String METHOD_PUT = "PUT";
    private static final String METHOD_TRACE = "TRACE";

    private static final String HEADER_IFMODSINCE = "If-Modified-Since";
    private static final String HEADER_LASTMOD = "Last-Modified";

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        String method = request.getMethod().toUpperCase();

        if (METHOD_GET.equals(method)) {
            long lastModified = this.getLastModified(request);
            if (lastModified == -1) {
                this.doGet(request, response);
            } else {
                long ifModifiedSince = request.getDateHeader(HEADER_IFMODSINCE);
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {
                    this.maybeSetLastModified(response, lastModified);
                    this.doGet(request, response);
                } else {
                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }
        } else if (METHOD_HEAD.equals(method)) {
            long lastModified = this.getLastModified(request);
            this.maybeSetLastModified(response, lastModified);
            this.doHead(request, response);
        } else if (METHOD_POST.equals(method)) {
            this.doPost(request, response);
        } else if (METHOD_PUT.equals(method)) {
            this.doPut(request, response);
        } else if (METHOD_DELETE.equals(method)) {
            this.doDelete(request, response);
        } else if (METHOD_OPTIONS.equals(method)) {
            this.doOptions(request, response);
        } else if (METHOD_TRACE.equals(method)) {
            this.doTrace(request, response);
        } else {
            response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "http method:" + method + " not implemented");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        throw new UnsupportedOperationException("http method:GET not implemented");
    }

    protected void doHead(HttpServletRequest request, HttpServletResponse response) {
        throw new UnsupportedOperationException("http method:HEAD not implemented");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        throw new UnsupportedOperationException("http method:POST not implemented");
    }

    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        throw new UnsupportedOperationException("http method:PUT not implemented");

    }

    protected void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        throw new UnsupportedOperationException("http method:DELETE not implemented");
    }

    protected void doOptions(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        throw new UnsupportedOperationException("http method:OPTIONS not implemented");
    }

    protected void doTrace(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        throw new UnsupportedOperationException("http method:TRACE not implemented");
    }

    protected long getLastModified(HttpServletRequest request) {
        return -1;
    }

    private void maybeSetLastModified(HttpServletResponse response, long lastModified) {
        if (response.containsHeader(HEADER_LASTMOD))
            return;
        if (lastModified >= 0)
            response.setDateHeader(HEADER_LASTMOD, lastModified);
    }
}
